package com.huqu.beans.hoidap;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N300Dao;
import com.huqu.entities.N300;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name="hoidapchitietBean")
@ViewScoped
public class HoiDapChiTietBean implements Serializable{
	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang="VN";
	
	private String md5baiviet="";
	private boolean havenews=true;
	private N300 question=new N300();
	private String title="";
	
	@PostConstruct
	public void init(){
		try {
			System.out.println("====== NewsBean =======");
			// kiểm tra ngôn ngữ
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG)!=null){
				vLang=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG).toString().toUpperCase();
				
				if(vLang.trim().toUpperCase().equals("VN")){
					if(sessionWeb.locale.equals(Locale.US)){
						sessionWeb.setLocale(new Locale("vi", "VN"));
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(new Locale("vi", "VN"));
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}else if(vLang.trim().toUpperCase().equals("EN")){
					if(sessionWeb.locale.equals(new Locale("vi", "VN"))){
						sessionWeb.setLocale(Locale.US);
						FacesContext.getCurrentInstance()
		    			.getViewRoot().setLocale(Locale.US);
						RequestContext.getCurrentInstance().execute( "location.reload();");
					}
				}
			}else{
				System.out.println("---language khong xac dinh ");
			}
			
			if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_IDBAIVIET)!=null){
				md5baiviet=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_IDBAIVIET);
				System.out.println("---idbaiviet= "+md5baiviet);
				
				//Lấy thông tin của câu hỏi 
				getInfoQuestion();

			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// load thông tin chi tiết của bài viết
	public void getInfoQuestion(){
		try {
			N300Dao n300Dao = (N300Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			System.out.println("---ListOfTabN300INFO( "+md5baiviet+" , "+ vLang+" )");
			List<N300> listN300INFO=new ArrayList<N300>();
			listN300INFO=n300Dao.ListOfTabN300INFO(md5baiviet, vLang);
			System.out.println("ListN300INFO.size()= "+listN300INFO.size());
			if(listN300INFO.size()>0){
				question=listN300INFO.get(0);
				title=question.getTieude();
				havenews=true;
			}else{
				havenews=false;
				if(vLang.toLowerCase().equals("VN")){
					title="The posts don't exits.";
				}else{
					title="Bài viết không tồn tại.";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}

	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public boolean isHavenews() {
		return havenews;
	}

	public void setHavenews(boolean havenews) {
		this.havenews = havenews;
	}

	public N300 getQuestion() {
		return question;
	}

	public void setQuestion(N300 question) {
		this.question = question;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	

}
